package com.lq.ecoldchain.ui.main.my

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.BaseNavigator
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.entity.UserInfoBean
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.PrefsHelper

/*
*2018/12/25 0025  17:58
*function by lq
*/
class MineViewModel(val prefsHelper: PrefsHelper) : BaseViewModel() {
    val user by lazy { MutableLiveData<UserInfoBean>() }
    val money by lazy { MutableLiveData<String>() }
    val refreshing by lazy { MutableLiveData<Boolean>() }
    init {
        requestUserInfo()//获取用户信息
        requestAccountInfo()
    }

    fun refreshData(){
        refreshing.value = true
        requestUserInfo()//获取用户信息
        requestAccountInfo()
    }

    private fun requestAccountInfo() {
        BaseNetCore.ApiServiceImpl.requestAccountInfo().compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<Double>(){
            override fun onSuccess(t: Double?) {
                money.value = "$t"
            }

            override fun onFail(code: String) {}

        })
    }

    private fun requestUserInfo() {
        BaseNetCore.ApiServiceImpl.requestUserInfo().compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<UserInfoBean>() {
            override fun onSuccess(t: UserInfoBean?) {
                user.value = t!!
                refreshing.value = false
            }

            override fun onFail(code: String) {
                refreshing.value = false
            }
        })
    }

     fun signOut(){
        BaseNetCore.ApiServiceImpl.signOut().compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<String>() {
            override fun onSuccess(t: String?) {
                prefsHelper.autoLogin = false
                BaseNavigator.toOther(BaseNavigator.login)
            }

            override fun onFail(code: String) {}
        })
    }
}